# To change this template, choose Tools | Templates
# and open the template in the editor.
import math
import pygame
from pygame import *
import string

SWIDTH = 800
SHEIGHT = 600
KEYLIST = ['UP','DOWN','LEFT','RIGHT','W','A','S','D','Q','E','N','M','SHIFT']

IDLE = 0
RUNNING = 1



R = (196,24,26)
O = (218,164,2)
Y = (191,218,2)
G = (20,218,2)
B = (2,218,216)
I = (108,2,218)
V = (188,2,218)
RAINBOW = [G,B,I,V,R,O,Y,(255,255,255)]

R2 = (254,211,186)
O2 = (254,237,186)
Y2 = (246,254,186)
G2 = (195,254,186)
B2 = (186,254,254)
I2 = (186,203,254)
V2 = (219,186,254)
RAINBOW2 = [R2,O2,Y2,G2,B2,I2,V2]

R3 = (253,0,77)
O3 = (253,209,77)
Y3 = (255,253,77)
G3 = (100,253,77)
B3 = (77,157,255)
I3 = (255,83,249)
V3 = (228,0,255)
RAINBOW3 = [R3,O3,Y3,G3,B3,I3,V3]

BORDERCOLOR = (100,100,100)
DEATHCOLOR = (200,0,0)
WARPCOLOR = (0,200,0)
KINGSPOTCOLOR = (240,240,240)

P0UNITCOLOR = (30  ,30,30)
P1UNITCOLOR = (225,225,225)


P0UNITSELECTCOLOR = (75,75,75)
P1UNITSELECTCOLOR = (180,180,180)

P0_AVAILABLE_MOVE_SPACE_COL = (242,212,212)
P1_AVAILABLE_MOVE_SPACE_COL = (212,212,242)

UNIT_MOVE_TIME = 800.0


def dx(d,theta):
    return d*math.cos(theta)
def dy(d,theta):
    return d*math.sin(theta)
def angleDiff(theta1,theta2):
    dif = theta2-theta1
    while(dif<-PI):
        dif+=2.0*PI
    while(dif>PI):
        dif -=2.0*PI
    #print "DIF%f"%dif
    return dif

def dist(pt1,pt2):
    dy = float(pt2[1]-pt1[1])
    dx = float(pt2[0]-pt1[0])
    #print math.sqrt(dy*dy+dx*dx)
    return math.sqrt(dy*dy+dx*dx)

def angleBetweenPts(pt1,pt2):
    return math.atan2(pt2[1]-pt1[1],pt2[0]-pt1[0])

def rotInDirection(theta1,theta2,change):
    dif = angleDiff(theta1,theta2)



def get_key():
    while 1:
        event = pygame.event.poll()
        if event.type == KEYDOWN:
            return event.key
        else:
            pass

def display_box(screen, message):
    "Print a message in a box in the middle of the screen"
    fontobject = pygame.font.Font(None, 18)
    pygame.draw.rect(screen, (0, 0, 0),
                     ((screen.get_width() / 2) - 100,
                     (screen.get_height() / 2) - 10,
                     200, 20), 0)
    pygame.draw.rect(screen, (255, 255, 255),
                     ((screen.get_width() / 2) - 102,
                     (screen.get_height() / 2) - 12,
                     204, 24), 1)
    if len(message) != 0:
        screen.blit(fontobject.render(message, 1, (255, 255, 255)),
                    ((screen.get_width() / 2) - 100, (screen.get_height() / 2) - 10))
    pygame.display.flip()

def ask(screen, question):
    "ask(screen, question) -> answer"
    pygame.font.init()
    current_string = []
    display_box(screen, question + ": " + string.join(current_string, ""))
    while 1:
        inkey = get_key()
        if inkey == K_BACKSPACE:
            current_string = current_string[0:-1]
        elif inkey == K_RETURN:
            break
        elif inkey == K_MINUS:
            current_string.append("_")
        elif inkey <= 127:
            current_string.append(chr(inkey))
        display_box(screen, question + ": " + string.join(current_string, ""))
    return string.join(current_string, "")

